<?php
	class Main extends CI_Controller {
	   
	   function __construct() {
	       parent::__construct();
           $this->load->model('dataset_db');
           $this->load->model('users/users_db','User');
	   }
       
       function login() {
            $data = array();
            if ($this->session->userdata('user')) redirect('/');
                        
            if($this->input->post("username")) {
                $data["username"] = $this->input->post("username",true);
                $data["password"] = md5($this->input->post("password",true));
                $objuser = $this->User->getUser($data);
                //print_object($objuser); exit;                
                if($objuser) {
                    if($objuser["disabled"] == 1) {
                        $data["errors"] = array(lang("user_disabled"));    
                    } else {
                        if ($this->input->post('rememberme')) {
    						$this->session->sess_destroy();
    						$this->config->set_item('sess_expiration', -1);
    						$this->session->sess_run();
    						$this->session->set_userdata('sess_length',(60*60*24*14));
    					}					
    					$this->dataset_db->delete_temp_data();												
    					$this->session->set_userdata('user', $objuser);
                        redirect('/');    
                    }
                    
                }
                else {
                    $data["errors"] = array(lang("user_invalid"));
                }
            }
            
            $this->load->view('login',$data);
       }
       
       function alogin() {
            $this->load->view('logout_alert',$data); 
       }
       
       
       
       function logout() {
            $this->dataset_db->delete_temp_data();
            $this->session->sess_destroy();
            redirect('login');
       }
       
       function index() {
            $user = $this->session->userdata('user');
            if (!$user) redirect('login');
            
            $navResult = $this->dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            
            //print_object($data); exit;
            $data['content'] = $this->load->view('dashboard',$data,true);
            $this->load->vars($data);
            $this->load->view('default_view');
       }
       
       function error_404() {
            $this->load->view('error_404',$data);
       } 
       
	}
    
/* End of file */